<template>
  <div>
    <k-form-build ref='kfb' @submit='submitData' :value='jsonData' :dynamicData="dynamicData" :disabled="disabled" />
    <div v-if='task' :wrapperCol="{ span: 24 }" style="text-align: center">
      <a-button v-if='task' type="primary"  @click="passTask">通过</a-button>
      <a-button v-if='task' style="margin-left: 8px"  @click="backTask">驳回</a-button>
    </div>
  </div>

</template>
<script>
import { saveFormDesignData, actBusinessAdd, getFormDesignDataById, getFormDesignDataHongById, dynamicData } from '@/api/api'
import { deleteAction, getAction,downFile } from '@/api/manage'
export default {
  name: 'FormDesignView',
  props: {
    /*全局禁用，可表示查看*/
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    /*表单格式数据*/
    /*jsonData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false
    },
    /!*表单数据*!/
    formDesignViewData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false
    },*/
    /*流程数据*/
    processData: {
      type: Object,
      default: () => {
        return {}
      },
      required: false
    },
    /*是否新增*/
    isNew: { type: Boolean, default: false, required: false },
    /*是否处理流程*/
    task: { type: Boolean, default: false, required: false }
  },
  data() {
    return {
      dynamicData: {
        currUserName: "ddddd",
        currUserID: "dddd",
        currUserDeptName: "dddd",
        currUserDeptID: [{
          label: "动态数据1",
          value: 1,
          def: true
        },
          {
            label: "动态数据2",
            value: 2
          },
          {
            label: "动态数据3",
            value: 3
          }]
      },
      jsonData: {},
      formDesign: this.$route.query.data,
      formDesignViewData: {},
      formDesignData: {}
    }
  },
  mounted () {
    console.log("流程数据",this.processData)
    this.init();
  },
  created () {
    /*console.log("流程数据",this.processData)
    this.init();*/
  },
  methods: {
    init() {
      //获取动态数据
      dynamicData().then(res=> {
        console.log("动态数据",res.result)
        if (res.result) {
          //this.dynamicData = res.result
        }
      })
      console.log("init流程数据",this.processData)
      getAction("/formDesign/formNub/" + this.processData.formDesignNumber).then(res => {
          this.formDesignViewData = res.result
          this.jsonData = JSON.parse(res.result.data)
          if (!this.isNew) {
            getFormDesignDataById(this.processData.formDesignDataId).then(formDesignDataRes => {
              this.formDesignViewData = formDesignDataRes
              this.$refs.kfb.form.setFieldsValue((JSON.parse(formDesignDataRes.result.fromData)))
            })
          } else {
            //获取宏数据
            getFormDesignDataHongById(this.processData.formDesignNumber).then(resHong => {
              console.log(resHong)
              this.$refs.kfb.form.setFieldsValue(resHong.result)
            })
          }
        })
    },
    //提交表单数据
    submitData(getData) {
      // 获取表单数据
      let formDesignDataForm = {}
      getData().then(values => {
        console.log(values)
        formDesignDataForm = {
          fromData: JSON.stringify(values),
          fromNumber: this.formDesignViewData.fromNumber,
          type: this.formDesignViewData.type,
          version: this.formDesignViewData.version,
          formDesignId: this.formDesignViewData.id,
          actId: this.processData.id
        }
        saveFormDesignData(formDesignDataForm).then(res => {
          if (res.code == 200) {
            this.$message.success(res.message)
            let formData = {
              procDefId: this.processData.id,
              procDeTitle: this.processData.name,
              tableName: this.processData.businessTable,
              filedNames: '',
              formDesignDataId: res.result.id
            }
            console.log(formData)
            actBusinessAdd(formData).then(res => {
              console.log(res)
              if (res.code == 200) {
                this.$message.success(res.message)
                this.$emit('afterSubmit',formData)
              } else
                this.$message.error(res.message)
            })

          } else
            this.$message.error(res.message)
        })

      })
        .catch((e) => {
          console.log(e)
          console.log('验证未通过，获取失败')
        })
    },
    /*通过审批*/
    passTask() {
      this.$emit('passTask')
    },
    /*驳回审批*/
    backTask() {
      this.$emit('backTask')
    }



  }
}
</script>